//
//  OutputItem.swift
//  DSP
//
//  Created by tucici on 2024/5/15.
//

import UIKit
private enum CellType{
    case Num
    case CH
}
class OutputItem: UICollectionViewCell {
    private var collectionView : UICollectionView!
    private var array = [CellType]()
    var longPressBlock : (()->Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
        initData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        cornerRadius = 4.k_h
        borderHexColor = HEX_BLACK
        borderWidth = 1
        
        let flowlayout = UICollectionViewFlowLayout()
        flowlayout.scrollDirection = .vertical
        collectionView = UICollectionView(frame:.zero, collectionViewLayout:flowlayout)
        collectionView.backgroundColor = .clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.isUserInteractionEnabled = false
        collectionView.register(NumItem.self, forCellWithReuseIdentifier: NumItem.ID())
        collectionView.register(CHItem.self, forCellWithReuseIdentifier: CHItem.ID())
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    func initData(){
        array = Array(repeating: .Num, count: 9)
        array.insert(.CH, at: 4)
        collectionView.reloadData()
    }
    
    @objc func actionForlongPress(_ sender:UILongPressGestureRecognizer){
        if sender.state == .began{
            longPressBlock?()
            let feedbackGenerator = UIImpactFeedbackGenerator(style: .medium)
            feedbackGenerator.impactOccurred()
        }
    }
    
    var model = OutputModel(){
        didSet{
            collectionView.reloadData()
        }
    }
    
    lazy var longpress : UILongPressGestureRecognizer = {
        let long = UILongPressGestureRecognizer(target: self, action: #selector(actionForlongPress))
        contentView.addGestureRecognizer(long)
        return long
    }()
    
}

//MARK: - UICollectionViewDataSource
extension OutputItem:UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch array[indexPath.row]{
        case .Num:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier:NumItem.ID(), for:indexPath) as! NumItem
            let i = indexPath.row < 4 ? indexPath.row : indexPath.row - 1
            cell.title.text = model.inputArr.count <= i || model.inputArr[i] == "" ? "" : "\(model.inputArr[i].last!)"
            cell.title.textHexColor = !model.enable ? HEX_TEXT_DISABLE : (model.select ? HEX_RED : HEX_BLACK)
            cell.title.borderHexColor = model.enable ? HEX_BLACK : HEX_BORDER_DISABLE
            return cell
        case .CH:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier:CHItem.ID(), for:indexPath) as! CHItem
            cell.title.text = model.name
            //cell.title.textHexColor = !model.enable ? HEX_TEXT_DISABLE : HEX_BLACK
            cell.title.bgHexColor = !model.enable ? HEX_BG : (model.select ? HEX_RED : HEX_BG)
            cell.title.borderHexColor = !model.enable ? HEX_BORDER_DISABLE : (model.select ? HEX_RED : HEX_BG)
            return cell
        }
    }
}
//MARK: - UICollectionViewDelegate
extension OutputItem:UICollectionViewDelegate
{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
    }
}
//MARK: - UICollectionViewDelegateFlowLayout
extension OutputItem:UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        let w = collectionView.bounds.width
        let h = collectionView.bounds.height
        switch array[indexPath.row]{
        case .Num:
            return CGSize(width: w / 4.0, height: h / 3.0)
        case .CH:
            return CGSize(width: w, height: h / 3.0)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{
        return 0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{
        return .zero
    }
}
